/*
 *  de.karnik.helper - Library for basic helper functions
 *  Copyright (C) 2007-2008 Markus Karnik
 *
 *  This program is free software; you can redistribute it and/or modify it 
 *  under the terms of the GNU General Public License as published by the
 *  Free Software Foundation; either version 3 of the License, 
 *  or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 *  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
 *  for more details.
 *
 *  You should have received a copy of the GNU General Public License along 
 *  with this program; if not, see <http://www.gnu.org/licenses/>.
 *  
 */
package de.karnik.helper;

/**
 * Useful funcitons to handle the system log.
 * 
 * @author Markus Karnik - markus.karnik@my-design.org
 * @version 1.0
 * @since 1.0
 */
public class LogFunctions {

	/**
	 * Status value of log level 1.
	 */
	public static int LOG_LEVEL_1 = 0;
	/**
	 * Status value of log level 2.
	 */
	public static int LOG_LEVEL_2 = 1;
	/**
	 * Status value of log level 3.
	 */
	public static int LOG_LEVEL_3 = 2;
	/**
	 * The max. length of one line.
	 */
	public static int LOG_WIDTH = 100;
	
	/**
	 * The debug switch. <b>true</b> if log is enabled, <b>false</b> otherwise.
	 */
	public static boolean DEBUG = true;
	
	/**
	 * Indicates the log level.
	 * 
	 * @see #LOG_LEVEL_1
	 * @see #LOG_LEVEL_2
	 * @see #LOG_LEVEL_3
	 */
	public static final int logLevel = LOG_LEVEL_2;
	
	/**
	 * The default constructor. Invisible.
	 */
	private LogFunctions() {}
	
	/**
	 * Tests the vm-name and prints the message to the vm specific status log.
	 * 
	 * @param data The message to print.
	 */
	private static void writeToSystemLog( String data ) {
			System.out.println( data  );
	}
	
	/**
	 * Prints a status or log message to the system log. <b>This function will ignore input until DEBUG is true</b>.
	 * 
	 * @param function The function name.
	 * @param state The state description.
	 */
	public static void log( String function, String state ) {
		if( DEBUG )
			writeToSystemLog( "Function: " + function + " / State: " + state );
	}
	
	/**
	 * Prints a status or log message to the system log. <b>This function will ignore input until DEBUG is true</b>.
	 * 
	 * @param function The function name.
	 * @param state The state description.
	 * @param data The message to print.
	 */
	public static void log( String function, String state, String data ) {
		if( DEBUG )
			if( data != null ) {
				writeToSystemLog( "Function: " + function + " / State: " + state );
				writeToSystemLog( "   " + data );
				
			} else {
				writeToSystemLog( "Function: " + function + " State: " + state );
				writeToSystemLog( "    NULLPOINTER!!!" );
			}
	}
	
	/**
	 * Prints a integer value to the system log. <b>This function will ignore input until DEBUG is true</b>.
	 * 
	 * @param function The function name.
	 * @param state The state description.
	 * @param data The integer value to print.
	 */
	public static void log( String function, String state, int data ) {
		if( DEBUG )
				writeToSystemLog( "Function: " + function + " / State: " + state );
				writeToSystemLog( "   " + data );
	}
	
	/**
	 * Prints a long value to the system log. <b>This function will ignore input until DEBUG is true</b>.
	 * 
	 * @param function The function name.
	 * @param state The state description.
	 * @param data The long value to print.
	 */
	public static void log( String function, String state, long data ) {
		if( DEBUG )
				writeToSystemLog( "Function: " + function + " / State: " + state );
				writeToSystemLog( "   " + data );
	}
	
	
	/**
	 * Prints a multiple status or log message to the system log.
	 * <b>This function will ignore input until DEBUG is true</b>.
	 * 
	 * @param function The function name.
	 * @param state The state description.
	 * @param data The message array to print.
	 */
	public static void log( String function, String state, String[] data ) {
		if( DEBUG )
			if( data != null ) {
				writeToSystemLog( "Function: " + function + " / State: " + state );
				for( int i = 0; i < data.length; i++ )
					writeToSystemLog( "   " + data[ i ] );
				
			} else {
				writeToSystemLog( "Function: " + function + " State: " + state );
				writeToSystemLog( "    NULLPOINTER!!!" );
			}
	}
	
	/**
	 * Prints a multiple error (stack trace ) message to the system log.
	 * <b>This function will ignore input until DEBUG is true</b>.
	 * 
	 * @param function The function name.
	 * @param state The state description.
	 * @param data The stack trace element array to print.
	 */
	public static void log( String function, String state, StackTraceElement[] data ) {
		if( DEBUG )
			if( data != null ) {
				writeToSystemLog( "Function: " + function + " / State: " + state );
				for( int i = 0; i < data.length; i++ )
					writeToSystemLog( "   " + data[ i ].toString() );
				
			} else {
				writeToSystemLog( "Function: " + function + " State: " + state );
				writeToSystemLog( "    NULLPOINTER!!!" );
			}
	}
	
	
	/**
	 * Prints a single status or log message to the system log. <b>This function will ignore input until DEBUG is true</b>.
	 * 
	 * @param data The message to print.
	 */
	public static void log( String data ) {
		if( DEBUG )
			if( data != null )
				writeToSystemLog( data );
			else
				writeToSystemLog( "NULLPOINTER!!!" );
	}
	
}
